package org.szy.plugin.dev.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.noear.solon.data.annotation.Tran;
import org.szy.core.common.enums.CommonStatusEnum;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.plugin.dev.entity.DevMessage;
import org.szy.plugin.dev.entity.DevMessageUser;
import org.szy.plugin.dev.mapper.DevMessageMapper;
import org.szy.plugin.dev.param.DevMessageQueryParam;
import org.szy.plugin.dev.service.DevMessageService;
import org.szy.plugin.dev.service.DevMessageUserService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static org.szy.plugin.dev.entity.table.DevMessageTableDef.DEV_MESSAGE;

/**
 * 开发-站内消息Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-28 18:17:41
 **/
@ProxyComponent
public class DevMessageServiceImpl extends BaseServiceImpl<DevMessageMapper, DevMessage> implements DevMessageService {

    @Inject
    private DevMessageMapper devMessageMapper;
    @Inject
    private DevMessageUserService devMessageUserService;

    @Override
    public QueryWrapper qwByQuery(DevMessageQueryParam param) {
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(param.getColumnField())) {
            qw.select(param.getColumnField().split(","));
        }
        qw.and(DEV_MESSAGE.CATEGORY.eq(param.getCategory()));
        qw.and(DEV_MESSAGE.TITLE.like(param.getTitle()));
        qw.and(DEV_MESSAGE.WHOLE.eq(param.getWhole()));
        qw.and(DEV_MESSAGE.STATUS.eq(param.getStatus()));

        return qw;
    }

    @Override
    public Page<DevMessage> page(QueryPage<DevMessage> page, DevMessageQueryParam param) {
        QueryWrapper qw = qwByQuery(param);

        // 正序排序
        if (StrUtil.isNotEmpty(page.getAscField())) qw.orderBy(page.getAscField().split(","));
        // 倒叙排序
        if (StrUtil.isNotEmpty(page.getDescField())) {
            for (String desc : page.getDescField().split(",")) {
                qw.orderBy(new QueryColumn(desc).desc());
            }
        }

        return this.page(page, qw);
    }


    @Override
    @Tran
    public boolean delete(Long id) {
        // 执行删除
        boolean remove = this.removeById(id);
        if (remove) devMessageUserService.removeByMessageId(id);
        return remove;
    }

    @Override
    public boolean release(Long id) {
        DevMessage message = getById(id);
        message.setPublishTime(LocalDateTime.now());
        message.setStatus(CommonStatusEnum.ENABLE.getKey());
        buildDMU(message);
        return updateById(message);
    }

    /**
     * 构建消息用户关联表
     *
     * @param entity 消息对象
     */
    private void buildDMU(DevMessage entity) {
        devMessageUserService.removeByMessageId(entity.getId());
        List<DevMessageUser> maps = JSONUtil.toList(entity.getUserList(), DevMessageUser.class);
        List<DevMessageUser> userList = new ArrayList<>();
        maps.forEach((map) -> {
            DevMessageUser devMessageUser = new DevMessageUser();
            devMessageUser.setMessageId(entity.getId());
            devMessageUser.setReceiverId(map.getReceiverId());
            devMessageUser.setReceiverName(map.getReceiverName());
            userList.add(devMessageUser);
        });
        devMessageUserService.saveBatch(userList);
    }
}
